package com.link.base.base.mpconfig.controller;

import com.link.base.base.coupons.model.Coupons;
import com.link.base.base.mpconfig.model.HomePopUp;
import com.link.base.base.mpconfig.service.HomePopUpService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小程序首页弹框配置
 *
 * @author: 闫有霞
 * @date: 2019/12/19
 */
@Controller
@RequestMapping("/link/homePopUp")
public class HomePopUpController extends BasicController<HomePopUp> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private HomePopUpService homePopUpService;

    @Override
    public BasicService<HomePopUp> getBasicService() throws Exception {
        return homePopUpService;
    }

    /**
     * 获取弹框
     */
    @RequestMapping(value = "/getHomePop")
    @ResponseBody
    public Map<String, Object> getHomePop( Integer rowNum, HttpSession session,
                                                    HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            String appId = UserUtil.getUser().getMpAppId();
            Long accntId = UserUtil.getUser().getId();
            if (appId == null || accntId == null) {
                throw new BasicControllerException("appId accntId 不能为空！");
            } else {
                HomePopUp homePopUp = new HomePopUp();
                homePopUp.setAppId(appId);
                List<HomePopUp> list = homePopUpService.queryByAppId(homePopUp);
                List<HomePopUp> homePopUpList = new ArrayList<>();
                if (list.size() > 0) {
                    for (HomePopUp popUp : list) {
                        popUp.setAccntId(accntId);
                        /*如果弹框类型为跳转活动--》校验该客户是否参加该活动*/
                        if ("LinkActivity".equals(popUp.getType())) {
                            /*当在中间表中找到该客户参加过该活动的记录时 删除该弹框 否则保留该弹框*/
                            if (homePopUpService.checkAccntActivity(popUp).size() == 0) {
                                homePopUpList.add(popUp);
                            }
                            /*如果弹框类型为直接领券*/
                        } else if ("CollectCoupon".equals(popUp.getType())) {
                            /*查询该活动下的优惠券列表*/
                            List<Coupons> couList = new ArrayList<>();
                            List<Coupons> couponList = homePopUpService.getActivityCoupon(popUp);
                            if (couponList.size() > 0) {
                                /*遍历该活动的优惠券 ——校验该客户是否使用这优惠券*/
                                for (Coupons coupon : couponList) {
                                     /*如果使用则删除*/
                                    HomePopUp checkCou = new HomePopUp();
                                    checkCou.setAccntId(accntId);
                                    checkCou.setCouponId(coupon.getId());
                                     /*当在中间表中找到该客户领取过该优惠券的记录时 删除该优惠券 否则保留该优惠券*/
                                    if (homePopUpService.checkAccntCoupon(checkCou).size() == 0) {
                                        couList.add(coupon);
                                    }
                                }
                                /*将通过校验的优惠券返回给该活动*/
                                popUp.setCouponsList(couList);
                                homePopUpList.add(popUp);
                            }
                            /*如果弹框类型为跳转会员卡*/
                        } else if ("LinkMemberCard".equals(popUp.getType())) {
//                            /*当在中间表中找到该客户名下的会员ID领取会员卡的记录时 删除该弹框 否则保留该弹框*/
//                            MemberAccount memberAccount = new MemberAccount();
//                            memberAccount.setMemCardId(popUp.getLinkId());
//                            memberAccount.setAccntId(accntId);
//                            memberAccount = memberAccountService.getMemberByCardId(memberAccount);
//                            if (null == memberAccount) {
//                                homePopUpList.add(popUp);
//                            }
                            /*如果弹框类型为优惠券通知*/
                            // TODO: 2019-11-01 会员卡迁移之后的待修改
                        } else if ("CouponNotice".equals(popUp.getType())) {
                            /*查询该客户下待通知的优惠券按时间倒序查出*/
                            popUp.setRowNum(rowNum);
                            List<Coupons>  noticeCoupList = homePopUpService.getAccntCoupon(popUp);
                            popUp.setCouponsList(noticeCoupList);
                            homePopUpList.add(popUp);
                        }

                    }
                }
                result.put("result", homePopUpList);
                result.put("success", true);
            }
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     *@createdBy 闫有霞
     *@date 2018/11/30
     *@methods: updateStatus
     *@param: HomePopUp homePopUp
     *@return:
     *@description: 更新状态
     **/

    @RequestMapping(value = "/updateStatus")
    @ResponseBody
    public Map<String, Object> updateStatus(@RequestBody HomePopUp homePopUp, HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            homePopUpService.updateStatus(homePopUp);
            result.put("success", true);
            result.put("result", homePopUp);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
